package com.cloud.system.api.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cloud.system.api.bean.SysConfigForm;
import com.cloud.system.api.entity.SysConfig;
import com.cloud.system.api.entity.SysInfo;
import com.cloud.system.api.mapper.SysConfigMapper;
import com.cloud.system.api.mapper.SysInfoMapper;
import com.cloud.system.api.service.SysConfigService;
import com.cloud.system.api.service.SystemCacheService;
import com.cloud.system.common.exception.BizException;

import tk.mybatis.mapper.entity.Example;

@Service
@Transactional
public class SysConfigServiceImpl implements SysConfigService {

	@Autowired
	private SysConfigMapper sysConfigMapper;

	@Autowired
	private SystemCacheService systemCacheService;

	@Autowired
	private SysInfoMapper sysInfoMapper;

	@Override
	public List<SysConfig> list() {
		return sysConfigMapper.selectAll();
	}

	@Override
	public SysConfigForm load(String configKey) {
		SysConfig cfg = sysConfigMapper.selectByPrimaryKey(new SysConfig(configKey));
		SysConfigForm form = new SysConfigForm();
		BeanUtils.copyProperties(cfg, form);
		form.setOldKey(cfg.getConfigKey());
		return form;
	}

	@Override
	public SysConfig saveUpdateConfig(SysConfigForm sysConfig) throws BizException {
		if (StringUtils.isEmpty(sysConfig.getOldKey())) {
			// 新增
			if (sysConfigMapper.existsWithPrimaryKey(sysConfig.getConfigKey())) {
				throw new BizException(500, "已经存在键值为:" + sysConfig.getConfigKey() + " 的配置项。");
			}
			sysConfigMapper.insert(sysConfig);
		} else {
			// 修改
			if (!sysConfig.getOldKey().equals(sysConfig.getConfigKey())) {
				SysConfig cfg = new SysConfig();
				cfg.setConfigKey(sysConfig.getConfigKey());
				if (sysConfigMapper.existsWithPrimaryKey(cfg)) {
					throw new BizException(500, "数据库中已经存在键值为:" + sysConfig.getConfigKey() + " 的配置项。");
				}
			}
			Example example = new Example(SysConfig.class);
			Example.Criteria criteria = example.createCriteria();
			criteria.andEqualTo("configKey", sysConfig.getOldKey());
			sysConfigMapper.updateByExampleSelective(sysConfig, example);
		}
		systemCacheService.evitSysConfigItemCache(sysConfig.getConfigName());
		return sysConfig;
	}

	@Override
	public String delete(String configKey) {
		sysConfigMapper.deleteByPrimaryKey(configKey);
		return configKey;
	}

	@Override
	public Object getSysConfigValue(Long sysId, String defaultHumanLoginPassword, Object defaultValue, Class<?> clazz) {
		SysConfig cfg = sysConfigMapper.selectByPrimaryKey(defaultHumanLoginPassword);
		if (null == cfg || StringUtils.isEmpty(cfg.getConfigValue())) {
			return defaultValue;
		}
		String value = cfg.getConfigValue();
		if (clazz.equals(String.class)) {
			return value;
		} else if (clazz.equals(Integer.class)) {
			return Integer.valueOf(value);
		} else if (clazz.equals(Long.class)) {
			return Long.valueOf(value);
		} else if (clazz.equals(Double.class)) {
			return Double.valueOf(value);
		}
		return value;
	}


	@Override
	public String getSystemUrl() {
		return sysConfigMapper.selectByPrimaryKey("SYSTEM_URL").getConfigValue();
	}

}
